package tesys.persistence;

import java.util.ArrayList;

import tesys.application.Main;
import tesys.objects.*;

public class DataAccessStub
{
	private String dbName;
	private String dbType = "stub";

	private ArrayList<User> users;
	private ArrayList<Task> tasks;
	private ArrayList<Project> projs;
	private ArrayList<Election> elections;
	private ArrayList<ElectionCandidate> candidates;
	private ArrayList<IterationTask> iterTasks;
	private ArrayList<Iteration> iterations;
	private ArrayList<ProjectMember> pms;

	public DataAccessStub(String dbName)
	{
		this.dbName = dbName;
	}

	public DataAccessStub()
	{
		this(Main.dbName);
	}

	public void open(String dbName)
	{
		User User;
		Task Task;
		SC mySC;

		users = new ArrayList<User>();
		User = new User("100", "Joe Cool", "UMSU");
		users.add(User);

		tasks = new ArrayList<Task>();
		Task = new Task("COMP3350", "Software Development");
		tasks.add(Task);

		scs = new ArrayList<SC>();
		mySC = new SC("100", "COMP3350", "Joe Cool", "Software Development", "A");
		scs.add(mySC);

		System.out.println("Opened " +dbType +" database " +dbName);
	}

	public void close()
	{
		System.out.println("Closed " +dbType +" database " +dbName);
	}

	public ArrayList<User> getUserSequential()
	{
		return users;
	}

	public ArrayList<User> getUserRandom(User currentUser)
	{
		ArrayList<User> newUsers;
		int index;
		
		newUsers = new ArrayList<User>();
		index = users.indexOf(currentUser);
		if (index >= 0)
		{
			newUsers.add(users.get(index));
		}
		return newUsers;
	}

	public String insertUser(User currentUser)
	{
		// don't bother checking for duplicates
		users.add(currentUser);
		return null;
	}

	public String updateUser(User currentUser)
	{
		int index;
		
		index = users.indexOf(currentUser);
		if (index >= 0)
		{
			users.set(index, currentUser);
		}
		return null;
	}

	public String deleteUser(User currentUser)
	{
		int index;
		
		index = users.indexOf(currentUser);
		if (index >= 0)
		{
			users.remove(index);
		}
		return null;
	}

	public ArrayList<Task> getTaskSequential()
	{
		return tasks;
	}

	public ArrayList<Task> getTaskRandom(Task currentTask)
	{
		ArrayList<Task> newTasks;
		int index;
		
		newTasks = new ArrayList<Task>();
		index = tasks.indexOf(currentTask);
		if (index >= 0)
		{
			newTasks.add(tasks.get(index));
		}
		return newTasks;
	}

	public String insertTask(Task currentTask)
	{
		// don't bother checking for duplicates
		tasks.add(currentTask);
		return null;
	}

	public String updateTask(Task currentTask)
	{
		int index;
		
		index = tasks.indexOf(currentTask);
		if (index >= 0)
		{
			tasks.set(index, currentTask);
		}
		return null;
	}

	public String deleteTask(Task currentTask)
	{
		int index;
		
		index = tasks.indexOf(currentTask);
		if (index >= 0)
		{
			tasks.remove(index);
		}
		return null;
	}

	public ArrayList<SC> getSC(SC currentSC)
	{
		ArrayList<SC> newSCs;
		SC sc;
		int counter;
		
		// get the SC objects with the same UserID as currentSC
		newSCs = new ArrayList<SC>();
		for (counter=0; counter<scs.size(); counter++)
		{
			sc = scs.get(counter);
			if (sc.getUserID().equals(currentSC.getUserID()))
			{
				newSCs.add(scs.get(counter));
			}
		}
		return newSCs;
	}

	public ArrayList<SC> getCS(SC currentSC)
	{
		ArrayList<SC> newSCs;
		SC cs;
		int counter;
		
		// get the SC objects with the same TaskID as currentSC
		newSCs = new ArrayList<SC>();
		for (counter=0; counter<scs.size(); counter++)
		{
			cs = scs.get(counter);
			if (cs.getTaskID().equals(currentSC.getTaskID()))
			{
				newSCs.add(scs.get(counter));
			}
		}
		return newSCs;
	}
}
